package com.cloud.encrypting_cloud_storage.service.impl;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.cloud.encrypting_cloud_storage.entity.User;
import com.cloud.encrypting_cloud_storage.exceptions.JsonException;
import com.cloud.encrypting_cloud_storage.service.AuthenticationService;
import com.cloud.encrypting_cloud_storage.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author leon
 * @Description: TODO
 * @date 2022年04月22日 10:35
 */
@Service
public class AuthenticationServiceImpl implements AuthenticationService {
    @Resource
    UserService userService;

    @Override
    public User authentication(User authenticationRequest) {
        String email = authenticationRequest.getName();
        byte[] verifyHash = authenticationRequest.getSha256VerifyKey();
        if(StrUtil.isBlank(email)){
            throw new JsonException(10001,"账号不能为空");
        }
        User user = findUserByEmail(authenticationRequest.getEmail());
        if(user==null){
            throw new JsonException(10001,"用户不存在");
        }
        if(!ArrayUtil.equals(authenticationRequest.getSha256VerifyKey(),user.getSha256VerifyKey())){
            throw new JsonException(10001,"密码错误");
        }
        return user;
    }

    @Override
    public User findUserByEmail(String email) {
        return userService.findUserByEmail(email);
    }
}
